<?php

namespace App\Models\WeChat;

use App\Models\Setting\Setting;
use EasyWeChat\Factory;
use EasyWeChat\MiniProgram\Application;
use Illuminate\Support\Facades\Redis;
use Symfony\Component\Cache\Adapter\RedisAdapter;

class WMP
{
    static public function getInstance($invoke): Application
    {
        $setting = Setting::where('category', $invoke)->pluck('value', 'key')->toArray();

        if (!\Arr::get($setting, 'app_id') || !\Arr::get($setting, 'secret')) {
            throw new \InvalidArgumentException('小程序参数未配置');
        }

        $app = Factory::miniProgram($setting);
        // 创建缓存实例
        $app->rebind('cache', new RedisAdapter(Redis::connection()->client()));

        return $app;
    }
}
